package org.zcl.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zcl.utils.QueryService;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * @Package org.zcl.service
 * @PROJECT_NAME: question
 * @DESCRIPTION:
 * @USER: zhuchenglin
 * @DATE: 2022/1/25 11:28
 */
@Service
public class QueryTypeService {

    @Autowired
    private TypeSerive typeSerive;

    private Map<String, Function<String, QueryService>> grantTypeMap=new HashMap<>();

    /**
     *  初始化业务分派逻辑,代替了if-else部分
     *  key: 优惠券类型
     *  value: lambda表达式,最终会获得该优惠券的发放方式
     */
    @PostConstruct
    public void dispatcherInit() {
        grantTypeMap.put("1", type -> typeSerive.queryMainService());
        grantTypeMap.put("2", type -> typeSerive.queryAsyncService());
        grantTypeMap.put("3", type -> typeSerive.queryService());
    }

    public QueryService getResult(String type) {
        //Controller根据 优惠券类型resourceType、编码resourceId 去查询 发放方式grantType
        Function<String, QueryService> result = grantTypeMap.get(type);
        if (result != null) {
            return result.apply(type);
        }
        return typeSerive.queryMainService();
    }

}
